package com.mysiteforme.admin.controller;

import com.mysiteforme.admin.entity.ElectricityHeadlines;
import com.mysiteforme.admin.entity.ElectricityPushFreemaker;
import com.mysiteforme.admin.service.ElectricityPushFreemakerService;
import com.xiaoleilu.hutool.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityPushFreemakerDesc;
import com.mysiteforme.admin.service.ElectricityPushFreemakerDescService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 微信推送模板明细表  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-10-22
 */
@Controller
@RequestMapping("/admin/electricityPushFreemakerDesc")
public class ElectricityPushFreemakerDescController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityPushFreemakerDescController.class);

    @Autowired
    private ElectricityPushFreemakerDescService electricityPushFreemakerDescService;
    @Autowired
    private ElectricityPushFreemakerService electricityPushFreemakerService;

    @GetMapping("list")
    @SysLog("跳转微信推送模板明细表列表")
    public String list(){
        return "/admin/electricityPushFreemakerDesc/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求微信推送模板明细表列表数据")
    public LayerData<ElectricityPushFreemakerDesc> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityPushFreemakerDesc> layerData = new LayerData<>();
        EntityWrapper<ElectricityPushFreemakerDesc> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag",false);
        if(!map.isEmpty()){
        }
        Page<ElectricityPushFreemakerDesc> pageData = electricityPushFreemakerDescService.selectPage(new Page<>(page,limit),wrapper);
        List<ElectricityPushFreemakerDesc> records = pageData.getRecords();
//        ElectricityPushFreemakerDesc electricityPushFreemakerDesc =new ElectricityPushFreemakerDesc();
        ArrayList<ElectricityPushFreemakerDesc> list =new ArrayList<>();
        for (ElectricityPushFreemakerDesc record : records) {
            EntityWrapper<ElectricityPushFreemaker> wrapper1 = new EntityWrapper<ElectricityPushFreemaker>();
            wrapper1.eq("id",record.getFreemakerId());
            ElectricityPushFreemaker electricityPushFreemaker = electricityPushFreemakerService.selectOne(wrapper1);
            record.setFreemakerName(electricityPushFreemaker.getFreemakerName());
            record.setStringId(String.valueOf(record.getId()));

            list.add(record);
        }
        layerData.setData(list);
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增微信推送模板明细表页面")
    public String add(){
        return "/admin/electricityPushFreemakerDesc/add";
    }
    @PostMapping("add")
    @SysLog("保存新增微信推送模板明细表数据")
    @ResponseBody
    public RestResponse add(ElectricityPushFreemakerDesc electricityPushFreemakerDesc){
        if(StringUtils.isBlank(electricityPushFreemakerDesc.getFreemakerId())){
            return RestResponse.failure("模板id不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemakerDesc.getWxCode())){
            return RestResponse.failure("微信模板字段key不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemakerDesc.getWxDesc())){
            return RestResponse.failure("微信模板字段描述不能为空");
        }

        ElectricityPushFreemaker electricityPushFreemaker = electricityPushFreemakerService.selectById(electricityPushFreemakerDesc.getFreemakerId());
        if(electricityPushFreemaker == null){
            return RestResponse.failure("未查询到模板");
        }
        electricityPushFreemakerDesc.setFreemakerId(electricityPushFreemaker.getId().toString());
        electricityPushFreemakerDesc.setFreemakerName(electricityPushFreemaker.getFreemakerName());
        electricityPushFreemakerDescService.insert(electricityPushFreemakerDesc);
        return RestResponse.success();
    }

    @RequestMapping("/queryMessagePushFormwork")
    @SysLog("新增获取消息模板")
    @ResponseBody
    public List<ElectricityPushFreemaker> getFormwork(){
        EntityWrapper<ElectricityPushFreemaker> wrapper = new EntityWrapper<ElectricityPushFreemaker>();
        List<ElectricityPushFreemaker>  list=electricityPushFreemakerService.selectList(wrapper);
        for (ElectricityPushFreemaker electricityPushFreemaker : list) {
            electricityPushFreemaker.setStringId(String.valueOf(electricityPushFreemaker.getId()));
        }
        return list;
    }

    @RequestMapping(value = "queryMessagePushFormwordDesc")
    @ResponseBody
    public Object query(String id){
        if(StringUtils.isEmpty(id)){
            return RestResponse.failure("参数非法，微信模板id不能为空");
        }
        EntityWrapper<ElectricityPushFreemaker> queryWrapper= new EntityWrapper<ElectricityPushFreemaker>();
        queryWrapper.eq("id", id);
        ElectricityPushFreemaker epf=electricityPushFreemakerService.selectOne(queryWrapper);

        EntityWrapper<ElectricityPushFreemakerDesc> descWrapper= new EntityWrapper<ElectricityPushFreemakerDesc>();
        descWrapper.eq("freemaker_id", epf.getId().toString());
        List<ElectricityPushFreemakerDesc> list = electricityPushFreemakerDescService.selectList(descWrapper);
        return list;
    }

    @GetMapping("edit")
    @SysLog("跳转编辑微信推送模板明细表页面")
    public String edit(Long id,Model model){
        ElectricityPushFreemakerDesc electricityPushFreemakerDesc = electricityPushFreemakerDescService.selectById(id);
        model.addAttribute("electricityPushFreemakerDesc",electricityPushFreemakerDesc);
        return "/admin/electricityPushFreemakerDesc/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑微信推送模板明细表数据")
    public RestResponse edit(ElectricityPushFreemakerDesc electricityPushFreemakerDesc){
        if(null == electricityPushFreemakerDesc.getId() || 0 == electricityPushFreemakerDesc.getId()){
            return RestResponse.failure("ID不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemakerDesc.getFreemakerId())){
            return RestResponse.failure("模板id不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemakerDesc.getWxCode())){
            return RestResponse.failure("微信模板字段key不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemakerDesc.getWxDesc())){
            return RestResponse.failure("微信模板字段描述不能为空");
        }

        electricityPushFreemakerDescService.updateById(electricityPushFreemakerDesc);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除微信推送模板明细表数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricityPushFreemakerDesc electricityPushFreemakerDesc = electricityPushFreemakerDescService.selectById(id);
        electricityPushFreemakerDesc.setDelFlag(true);
        electricityPushFreemakerDescService.updateById(electricityPushFreemakerDesc);
        return RestResponse.success();
    }

}
